#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
    int n = (nums1Size < nums2Size) ? nums1Size : nums2Size;
    int n1 = (nums1Size > nums2Size) ? nums1Size : nums2Size;

    int* p = (int*)malloc(sizeof(int) * n);
    int count = 0;
    int i = 0;
    int j = 0;
    int flag = 0;
    while (i < n1 && j < n)
    {
        if (nums1[i] == nums2[j] && n == nums2Size)
        {
            j++;
            if (nums1[i] != p[count - 1] || !count)
            {
                p[count++] = nums1[i];
                flag = 1;
            }

        }
        else if (nums1[j] == nums2[i] && n == nums1Size)
        {
            j++;
            if (nums2[i] != p[count - 1] || !count)
            {
                p[count++] = nums2[i];
                flag = 1;
            }
        }
        else if (flag && nums1[i] != nums2[j])
        {
            int* ret = (int*)realloc(p, sizeof(int) * count);
            if (ret)
            {
                *returnSize = count;
                return ret;
            }
        }
        i++;
    }
    if (!flag)return NULL;
    int* ret = (int*)realloc(p, sizeof(int) * count);
    *returnSize = count;
    return ret;

}
int main()
{
    int arr[] = { 4,9,5 };
    int arr1[] = { 9,4,9,8,4 };
    int sz1 = sizeof(arr) / sizeof(arr[0]);
    int sz = sizeof(arr1) / sizeof(arr1[0]);
    int x = 0;
    int* p = intersection(arr, sz1, arr1, sz, &x);
    return 0;
}